<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-search" size="mini" @click="handleSearch">搜索</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['production:bottleneck:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-edit" size="mini" @click="handleUpdate" :disabled="single" v-hasPermi="['production:bottleneck:edit']">
          修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['production:bottleneck:remove']">
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['production:bottleneck:export']">
          导出
        </el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      @scrollBottom="load"
      highlight-current-row
      border
      stripe
      v-loading="loading"
      :data="bottleneckList"
      @selection-change="handleSelectionChange"
      :header-cell-style="{ 'text-align': 'center' }">
      <el-table-column label="#" type="index" width="40" align="center" />
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" :show-overflow-tooltip="true" width="100" align="center" prop="blId" />
      <el-table-column label="机台" :show-overflow-tooltip="true" width="100" align="center" prop="blJtbh" />
      <el-table-column label="瓶颈物料" :show-overflow-tooltip="true" width="120" align="center" prop="blPjdm" />
      <el-table-column label="品名规格" :show-overflow-tooltip="true" width="350" align="center" prop="blPmgg" />
      <el-table-column label="目标产能" :show-overflow-tooltip="true" width="100" align="center" prop="blNum" />
      <!--      <el-table-column label="白班产能" :show-overflow-tooltip="true" width="100" align="center" prop="blBbsl"/>-->
      <!--      <el-table-column label="夜班产能" :show-overflow-tooltip="true" width="100" align="center" prop="blYbsl"/>-->
      <el-table-column label="建立人员" :show-overflow-tooltip="true" width="100" align="center" prop="blJlry" />
      <el-table-column label="建立日期" :show-overflow-tooltip="true" width="150" align="center" prop="blJlrq" />
    </el-table>

    <!-- 查询对话框   -->
    <el-dialog :title="title" v-model="openSearch" :close-on-click-modal="false" draggable width="650px" append-to-body>
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-col :span="24">
          <el-form-item label="建立时间" prop="dateRange">
            <el-date-picker
              clearable
              size="small"
              v-model="dateRange"
              :clearable="false"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="['00:00:00', '23:59:59']"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="机台编号" prop="blJtbh">
            <el-select v-model="queryParams.blJtbh" filterable>
              <el-option v-for="dict in jtmList" :key="dict.jtmJtbh" :label="dict.jtmJtbh" :value="dict.jtmJtbh"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="客户代码" prop="blPjdm">
            <el-input v-model="queryParams.blPjdm" placeholder="请输入客户代码" clearable size="small" @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <!--        <el-col :span="24">-->
        <!--          <el-form-item>-->
        <!--            <el-radio-group v-model="queryParams.moeZtbz">-->
        <!--              <el-radio label="1">工单未完成</el-radio>-->
        <!--              <el-radio label="2">工单已完成</el-radio>-->
        <!--              <el-radio label="-1">全部</el-radio>-->
        <!--            </el-radio-group>-->
        <!--          </el-form-item>-->
        <!--        </el-col>-->
        <el-col :span="24">
          <el-form-item style="margin-top: 25px; display: flex; justify-content: flex-end">
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>

    <!-- 添加或修改瓶颈物料对话框 -->
    <el-dialog :title="title" v-model="open" :close-on-click-modal="false" draggable width="680px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-col :span="12">
          <el-form-item label="机台" prop="blJtbh">
            <el-select v-model="form.blJtbh" @change="changeJt" :disabled="title === '修改瓶颈物料'" >
              <el-option v-for="dict in jtmList" :key="dict.jtmJtbh" :label="dict.jtmJtbh" :value="dict.jtmJtbh"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="产品编码" prop="blPjdm">
            <el-select
              v-model="form.blPjdm"
              :filter-method="getWldmListTOP50"
              @change="changeWldm"
              filterable
              placeholder="请选择产品编码"
              clearable
              size="small">
              <el-option v-for="dict in wldmList" :label="dict.itm_custwldm" :key="dict.itm_wldm" :value="dict.itm_wldm" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="品名规格" prop="blPmgg">
            <el-input v-model="form.blPmgg" readonly />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="目标产能" prop="blNum">
            <el-input-number style="width: 480px" v-model="form.blNum" placeholder="请输入数量" />
          </el-form-item>
        </el-col>
        <br />
        <br />
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBottleneck,
  getBottleneck,
  delBottleneck,
  addBottleneck,
  updateBottleneck,
  exportBottleneck,
  getMoeWldmByJtbh,
} from '@/api/production/bottleneck'
import { getItmMstrByCustwldm, getItmWldmAndCustWldmList } from '@/api/project/materiel'
import { listJtm } from '@/api/project/jtm'

export default {
  name: 'Bottleneck',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      // 总条数
      total: 0,
      // bottleneck表格数据
      bottleneckList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      dateRange: [new Date(new Date().getTime() - 1000 * 60 * 60 * 24 * 30), new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 30)],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        blPjdm: null,
        blJtbh: null,
        moeZtbz: '-1',
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        blJtbh: [{ required: true, message: '机台不能为空', trigger: 'blur' }],
        blPjdm: [{ required: true, message: '产品编码不能为空', trigger: 'blur' }],
        blNum: [{ required: true, message: '目标产能数量不能为空', trigger: 'blur' }],
      },
      itmwldmOptionsDatas: [],
      wldmList: [],
      jtmList: [],
    }
  },
  created() {
    //获取所有机台
    listJtm(null).then((res) => {
      this.jtmList = res.rows
    })

    // getItmWldmAndCustWldmList().then(res => {
    //   this.itmwldmOptionsDatas = res.data
    // })
    this.getList()
  },
  methods: {
    /** 查询瓶颈物料列表 */
    getList() {
      this.loading = true
      this.queryParams.pageNum = 1
      // this.queryParams.params = {}
      // if (this.dateRange !== null && this.dateRange != '') {
      //   this.queryParams.params['beginDate'] = this.moment(this.dateRange[0]).format('YYYY-MM-DD HH:mm:ss')
      //   this.queryParams.params['endDate'] = this.moment(this.dateRange[1]).format('YYYY-MM-DD HH:mm:ss')
      // }
      listBottleneck(this.queryParams).then((response) => {
        this.bottleneckList = []
        this.bottleneckList = this.bottleneckList.concat(response.rows)
        this.total = response.total
        this.loading = false
      })
    },

    //load
    load() {
      if (this.queryParams.pageNum < Math.ceil(this.total / this.queryParams.pageSize)) {
        this.queryParams.pageSize = 25
        this.queryParams.pageNum++
        listBottleneck(this.queryParams).then((response) => {
          this.bottleneckList = this.bottleneckList.concat(response.rows)
          this.total = response.total
          this.loading = false
        })
      }
      return 0
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.openSearch = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        blId: null,
        blPjdm: null,
        blJtbh: null,
        blNum: null,
        blPmgg: null,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.bottleneckList = []
      this.getList()
      this.openSearch = false
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.blId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.wldmList =[]
      //this.getWldmListTOP50('')
      this.open = true
      this.title = '填写瓶颈物料'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const blId = row.blId || this.ids
      getBottleneck(blId).then((response) => {
        this.form = response.data
        this.changeWldm()
        if (this.form.blJtbh) {
          getMoeWldmByJtbh(this.form.blJtbh).then((res) => {
            this.wldmList = res.data
            this.open = true
            this.title = '修改瓶颈物料'
          })
        }
      })
    },
    handleSearch() {
      this.reset()
      this.title = '查询瓶颈物料'
      this.openSearch = true
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.blId != null) {
            updateBottleneck(this.form).then((response) => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addBottleneck(this.form).then((response) => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const blIds = row.blId || this.ids
      this.$confirm('是否确认删除瓶颈物料编号为"' + blIds + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(function () {
          return delBottleneck(blIds)
        })
        .then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有瓶颈物料数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.exportLoading = true
          return exportBottleneck(queryParams)
        })
        .then((response) => {
          this.download(response.msg)
          this.exportLoading = false
        })
        .catch(() => {})
    },
    getWldmListTOP50(input) {
      let temp = []
      let that = this
      new Promise(function (resolve, reject) {
        for (let i of that.itmwldmOptionsDatas) {
          if (i.itmWldm.indexOf(input) != -1) {
            temp.push(i)
          }
        }
        resolve(temp)
      }).then((res) => {
        that.wldmList = res.slice(0, 50)
      })
    },
    changeWldm() {
      if (this.form.blPjdm) {
        getItmMstrByCustwldm(this.form.blPjdm).then((response) => {
          if (response.data) {
            this.form.blPmgg = response.data[0].itmPmgg
          } else {
            this.msgError('未查到物料信息')
          }
        })
      }
    },
    changeJt() {
      if (this.form.blJtbh) {
        getMoeWldmByJtbh(this.form.blJtbh).then((res) => {
          this.wldmList = res.data
        })
      }
    },
  },
}
</script>

<style lang="scss" scoped></style>
